package com.nowcoder.chapter3.part39;

import java.io.*;
import java.util.Scanner;

/**
 * 重定向
 */
public class IODemo8 {

    public static void main(String[] args) {
//        testRedirectOutput();
        testRedirectInput();
    }

    public static void testRedirectOutput() {
        try (
                PrintStream ps = new PrintStream(
                        new FileOutputStream("C:/work/alpha/out.txt"));
        ) {
            System.setOut(ps);
            System.out.println("白日依山尽,");
            System.out.println("黄河入海流.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void testRedirectInput() {
        try (
                FileInputStream fis = new FileInputStream("C:/work/alpha/out.txt");
        ) {
            System.setIn(fis);
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()) {
                System.out.println(scanner.next());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
